﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;


namespace DataBaseCreator
{
    [RunInstaller(true)]
    public partial class InstallerBuilder : System.Configuration.Install.Installer
    {
        public InstallerBuilder()
        {
            InitializeComponent();
            this.Committing+= new InstallEventHandler(CreateDataBase);
        }

        private void CreateDataBase(object sender, InstallEventArgs e)
        {
            if (!IsDataBaseInstalled())
            {
                string sqlConnectionString =
                    @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master;Data Source=.\SQLEXPRESS";
                Directory.SetCurrentDirectory(Path.GetDirectoryName
       (Assembly.GetExecutingAssembly().Location));
                string script = File.ReadAllText(Path.GetDirectoryName(
          Assembly.GetExecutingAssembly().Location) + @"\scriptBrigada.sql");
                // split script on GO command
                IEnumerable<string> commandStrings = Regex.Split(script, @"^\s*GO\s*$",
                                                                 RegexOptions.Multiline | RegexOptions.IgnoreCase);
                SqlConnection Connection = new SqlConnection(sqlConnectionString);
                Connection.Open();
                foreach (string commandString in commandStrings)
                {
                    if (commandString.Trim() != "")
                    {
                        new SqlCommand(commandString, Connection).ExecuteNonQuery();
                    }
                }
                Connection.Close();
            }
        }

        private bool IsDataBaseInstalled()
        {
            const string server = ".\\sqlexpress";
            const string database = "brigada";
            const string connString = ("Data Source=" + (server + ";Initial Catalog=master;Integrated Security=True;"));
            String cmdText = ("select * from master.dbo.sysdatabases where name=\'" + (database + "\'"));
            Boolean bRet;

            System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(connString);
            System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand(cmdText, sqlConnection);

            try
            {
                sqlConnection.Open();
                System.Data.SqlClient.SqlDataReader reader = sqlCmd.ExecuteReader();
                bRet = reader.HasRows;
                sqlConnection.Close();
            }
            catch (Exception e)
            {
                bRet = false;
                sqlConnection.Close();
            } //End Try Catch Block

            return bRet;
        }
    }
}
